package com.qf.shopping.service.impl;

import com.qf.shopping.dao.BaseDao;
import com.qf.shopping.dao.ICartDao;
import com.qf.shopping.dao.IProductDao;
import com.qf.shopping.dao.impl.CartDaoImpl;
import com.qf.shopping.dao.impl.ProductDaoImpl;
import com.qf.shopping.entity.Product;
import com.qf.shopping.entity.ShoppingCart;
import com.qf.shopping.service.ICartService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 三旬
 * @version 1.0  2022/9/22
 */

public class CartServiceImpl extends BaseServiceImpl<ShoppingCart> implements ICartService {

    private ICartDao cartDao = new CartDaoImpl();
    private IProductDao  productDao = new ProductDaoImpl();

    @Override
    protected BaseDao<ShoppingCart> getBaseDao() {
        return cartDao;
    }

    @Override
    public List<ShoppingCart> getMyCart(Integer uid) {
        //查询购物车的商品
        List<ShoppingCart> myCart = cartDao.selectProductByUid(uid);
        //通过商品pid查找到对应的商品信息
        for (ShoppingCart cart : myCart) {
            Integer pid = cart.getPid();
            Product product = productDao.selectOneById(pid);
            cart.setProduct(product);
        }
        return myCart;
    }

    @Override
    public boolean deleteByPidAndUid(Integer pid, Integer uid) {
        return cartDao.deleteByPidAndUid(pid,uid) > 0;
    }

    @Override
    public Map<String, Object> updateMyProductNums(Integer pid, Integer uid, Integer flag) {
        Map<String,Object> map = new HashMap<>();
        map.put("code",100);
        //获得该商品的库存
        Product product = productDao.selectOneById(pid);
        Integer stock = product.getStock();
        //获得原本购买商品的数量
        Integer nums = cartDao.selectProductByPidAndUid(pid,uid).getNums();
        //判断是增加还是减少
        if(flag > 0){
            //增加
            if (nums < stock) {
                nums =nums +1;
                map.put("code",200);
            } else {
                map.put("msg","别点了，库存不足了!");
                return map;
            }
        }else{
            //减少
            if (nums > 1) {
                nums =nums -1;
                map.put("code",200);
            } else {
                map.put("msg","选都选了，至少买一个吧？");
                return map;
            }
        }
        ShoppingCart cart = new ShoppingCart();
        cart.setNums(nums);
        cart.setUid(uid);
        cart.setPid(pid);
        Integer result = cartDao.updateNumsByPidAndUid(cart);
        if(result > 0){
            map.put("code",200);
        }
        return map;
    }

    /**
     * 添加商品到我的购物车中
     * @param cart
     * @return
     */
    @Override
    public Boolean addToMyCart(ShoppingCart cart) {
        //先判断商品在购物车中是否已经存在
        ShoppingCart productCart = cartDao.checkProductByUidAndPid(cart.getUid(),cart.getPid());
        if(productCart != null){
            //商品已经添加过了，只需要修改购物车中商品的数量
            Integer result = cartDao.addProductNumsByUidAndPid(cart);
            return  result > 0;
        }
        return  cartDao.insert(cart) > 0;
    }

    @Override
    public Integer getCartProductNumsByUid(Integer uid) {
        List<ShoppingCart> list = cartDao.getCartProductNumsByUid(uid);
        return list.size();
    }
}
